package com.test.work.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.test.work.dao.AdminMapper;
import com.test.work.dao.UserMapper;
import com.test.work.dto.DataTableDto;
import com.test.work.model.User;
import com.test.work.service.IAdminLoginService;

@Service
public class AdminLoginService implements IAdminLoginService {

	@Resource
	private AdminMapper adminDao;
	
	@Resource
	private UserMapper userDao;
	
	@Override
	public Object login(String username, String password) throws Exception {
		Integer i = 0;
		i = adminDao.login(username,password);
		if(i==null)
			return 0;
		else
			return 1;
	}

	@Override
	public Integer getfInfoCount(String search, Integer flag) throws Exception {
		Integer resultCount = 0;
		if (flag == null) {
			resultCount = this.userDao.getCount();
		}
		else if (flag == 0) {
			resultCount = this.userDao.getCount1();
		}
		else if (flag == 1){
			resultCount = this.userDao.getCount2();
		}
		return resultCount;
	}

	@Override
	public List<User> getfInfoDataTable(DataTableDto dataTableDto, Integer flag) throws Exception {
		Integer page = 0;
		if(dataTableDto.length!=-1) {
			dataTableDto.start = dataTableDto.start == null ? 1 : dataTableDto.start;
			dataTableDto.length = dataTableDto.length == null ? 10 : dataTableDto.length;
			page = (dataTableDto.start / dataTableDto.length) + 1; //第几页
			PageHelper.startPage(page, dataTableDto.length);
		}
		List<User> list = null;
		if (flag == null) {
			list =this.userDao.getDataTable();
		}
		else if (flag == 0) {
			list = this.userDao.getDataTable1();
		}
		else if (flag == 1) {
			list = this.userDao.getDataTable2();
		}
		return list;
	}

	@Override
	public Integer delete(String ids) throws Exception {
		String[] evaluationIdsArray=ids.split(",");
		int i = 0;
		for(i=0;i<evaluationIdsArray.length;i++){
			userDao.delete(Integer.parseInt(evaluationIdsArray[i]));
		}
		return 1;
	}

	@Override
	public Integer recover(String ids) throws Exception {
		String[] evaluationIdsArray=ids.split(",");
		int i = 0;
		for(i=0;i<evaluationIdsArray.length;i++){
			userDao.recover(Integer.parseInt(evaluationIdsArray[i]));
		}
		return 1;
	}

}
